home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / jaq / dist / tlog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-24  |  4.7 KB  |  228 lines

  1. /* 
  2.  * tlog.c--
  3.  *
  4.  *    Simple logging functions for Jaquith archive package.
  5.  *
  6.  * Copyright 1992 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  * 
  15.  * Quote:
  16.  *      The mass of men lead lives of quiet desperation.
  17.  *      -- Thoreau
  18.  */
  19.  
  20. #ifndef lint
  21. static char rcsid[] = "$Header: /sprite/lib/forms/RCS/taputils.c,v 1.0 91/01/07 18:02:37 mottsmth Exp $ SPRITE (Berkeley)";
  22. #endif /* not lint */
  23.  
  24. #include "jaquith.h"
  25.  
  26. /* file globals */
  27. static char printBuf[T_MAXSTRINGLEN];
  28. static FILE *activeLog = NULL;
  29. static int   activeLogDetail = LOG_FAIL;
  30.  
  31.  
  32. /*
  33.  *----------------------------------------------------------------------
  34.  *
  35.  * Log_SetLevel
  36.  *
  37.  *    Specify which events will appear in logfile.
  38.  *
  39.  * Results:
  40.  *    none.
  41.  *
  42.  * Side effects:
  43.  *    Sets global variable activeLogDetail.
  44.  *
  45.  * Presently, the logging stuff is set up as levels of 
  46.  * increased detail, but some day bits may divide the logging
  47.  * into types, controlled by a bit vector. 
  48.  *
  49.  *----------------------------------------------------------------------
  50.  */
  51.  
  52. void
  53. Log_SetDetail(detail)
  54.     int detail;               /* level of logging detail */
  55. {
  56.  
  57.     sprintf(printBuf, "Setting detail level to %d\n", detail);
  58.     Log_Event("Log_SetDetail", printBuf, LOG_MAJOR);
  59.  
  60.     activeLogDetail = detail;
  61.  
  62. }
  63.  
  64.  
  65.  
  66. /*
  67.  *----------------------------------------------------------------------
  68.  *
  69.  * Log_Open
  70.  *
  71.  *    Open a logging file.
  72.  *
  73.  * Results:
  74.  *    none.
  75.  *
  76.  * Side effects:
  77.  *    Consumes a file descriptor.
  78.  *
  79.  *----------------------------------------------------------------------
  80.  */
  81.  
  82. int
  83. Log_Open(logName)
  84.     char *logName;            /* name of logging file */
  85. {
  86.     char *pathName;
  87.  
  88.     if ((logName == NULL) || (*logName == '\0')) {
  89.     Utils_Bailout("LogOpen: got null ptr\n", BAIL_PRINT);
  90.     }
  91.     
  92.     if (activeLog != NULL) {
  93.     Log_Event("Log_Open", "Closing logfile\n", LOG_MAJOR);
  94.     Log_Close();
  95.     }
  96.     
  97.     pathName = Utils_MakeFullPath(logName);
  98.     activeLog = fopen(pathName, "a");
  99.     if (activeLog != NULL) {
  100.     sprintf(printBuf, "Opening log file %s\n", pathName);
  101.     Log_Event("LogOpen", printBuf, LOG_MAJOR);
  102.     }
  103.     MEM_FREE("Log_Open", pathName);
  104.  
  105.     return(errno);
  106.     
  107. }
  108.  
  109.  
  110. /*
  111.  *----------------------------------------------------------------------
  112.  *
  113.  * Log_Close
  114.  *
  115.  *    Closes the active logging file
  116.  *
  117.  * Results:
  118.  *    none.
  119.  *
  120.  * Side effects:
  121.  *    Frees a file descriptor.
  122.  *
  123.  *----------------------------------------------------------------------
  124.  */
  125.  
  126. int
  127. Log_Close()
  128. {
  129.  
  130.     if (activeLog != NULL) {
  131.     Log_Event("LogClose", "Closing log", LOG_MAJOR);
  132.     fclose(activeLog);
  133.     activeLog = NULL;
  134.     }
  135.  
  136.     return(errno);
  137.  
  138. }
  139.  
  140.  
  141. /*
  142.  *----------------------------------------------------------------------
  143.  *
  144.  * Log_Event
  145.  *
  146.  *    Record interesting events in the log file
  147.  *
  148.  * Results:
  149.  *    none.
  150.  *
  151.  * Side effects:
  152.  *    Puts output into logfile
  153.  *
  154.  *----------------------------------------------------------------------
  155.  */
  156.  
  157. int
  158. Log_Event(src, msg, detail)
  159.     char *src;                /* name of caller */
  160.     char *msg;                /* log message */
  161.     int detail;               /* importance of message */
  162. {
  163.     time_t currentTime;
  164.  
  165.     if ((activeLog == NULL) || (detail > activeLogDetail)) {
  166.     return(T_FAILURE);
  167.     }
  168.  
  169.     currentTime = Time_GetCurDate();
  170.  
  171.     fprintf(activeLog, "%s %s: %s", 
  172.         Time_CvtToString(¤tTime), src, msg);
  173.     if (msg[strlen(msg)-1] != '\n') {
  174.     fputc('\n', activeLog);
  175.     }
  176.     fflush(activeLog);
  177.     return(0);
  178.  
  179. }
  180.  
  181. /*
  182.  *----------------------------------------------------------------------
  183.  *
  184.  * Log_AtomicEvent
  185.  *
  186.  *    Record interesting events in a shared log file
  187.  *
  188.  * Results:
  189.  *    none.
  190.  *
  191.  * Side effects:
  192.  *    Puts output into logfile
  193.  *
  194.  *----------------------------------------------------------------------
  195.  */
  196.  
  197. int
  198. Log_AtomicEvent(src, msg, logName)
  199.     char *src;                /* name of caller */
  200.     char *msg;                /* log message */
  201.     char *logName;            /* name of shared log file */
  202. {
  203.     time_t currentTime;
  204.     FILE *logStream;
  205.     int pid = getpid();
  206.     Lock_Handle lockHandle;
  207.  
  208.     currentTime = Time_GetCurDate();
  209.  
  210.     if (Lock_Acquire(logName, LOCK_BLOCK, &lockHandle) != T_SUCCESS) {
  211.     return T_FAILURE;
  212.     }
  213.  
  214.     if ((logStream=fopen(logName, "a")) != (FILE *)NULL) {
  215.     fprintf(logStream, "%s %s (0x%x): %s",
  216.         Time_CvtToString(¤tTime), src, pid, msg);
  217.     if (msg[strlen(msg)-1] != '\n') {
  218.         fputc('\n', logStream);
  219.     }
  220.     fclose(logStream);
  221.     }
  222.  
  223.     Lock_Release(&lockHandle);
  224.  
  225.     return T_SUCCESS;
  226.  
  227. }
  228.